/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package DAO;

import Classes.BusinessCase;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.Vector;
import java.util.logging.Level;
import java.util.logging.Logger;

/**
 *
 * @author alexander
 */
public class BusinessCaseDAO implements BusinessCaseDAOInterface{
    
    //final static String dbAddress = "jdbc:mysql://a0752765mysql1.mysql.univie.ac.at:3306/a0752765mysql1";
    //final static String dbUser = "a0752765";    
    //final static String dbUserPassword = "bmjzphcs";
    
    final static String dbAddress = "jdbc:mysql://127.0.0.1:3306/immoDB";
    final static String dbUser = "root";    
    final static String dbUserPassword = "geheim";
    

    @Override
    public boolean saveBusinessCase(BusinessCase businessCase) {
        
        
        java.util.Date today = new java.util.Date();
        java.sql.Timestamp ts1 = new java.sql.Timestamp(today.getTime());
        
        int userID = businessCase.getBuyerID();
        int immoID = businessCase.getImmoID();
        String timeStamp = businessCase.getTimestamp();
        
        
        
        String query="INSERT INTO USER_BOUGHT_IMMO(ID_User,ID_IMMO,BusinessCaseDate)VALUES('"+userID+"','"
                       +immoID+"','"+ts1+"')" ;

        try {
            Class.forName("com.mysql.jdbc.Driver");
        } catch (ClassNotFoundException ex) {
            Logger.getLogger(UserDAO.class.getName()).log(Level.SEVERE, null, ex);
        }
        Connection con;
        try {
            con = DriverManager.getConnection(dbAddress, dbUser, dbUserPassword);
            java.sql.Statement stmt = con.createStatement();
            int rs = stmt.executeUpdate(query);
            
       } catch (SQLException ex) {
            Logger.getLogger(UserDAO.class.getName()).log(Level.SEVERE, null, ex);
        }

        return true;

        
        
        
    }

    @Override
    public Vector<BusinessCase> readBusinessCase() {
        Vector<BusinessCase> allBusinessCases = new Vector<BusinessCase>();
        
        String query = "Select * from USER_BOUGHT_IMMO";
        
        Vector<BusinessCase> newResultVector = new Vector<BusinessCase>();
        
    try {
            Class.forName("com.mysql.jdbc.Driver");
        } catch (ClassNotFoundException ex) {
            System.out.println("Treiberklasse nicht gefunden");
            Logger.getLogger(ImmoDAO.class.getName()).log(Level.SEVERE, null, ex);
        }
       try {

           Connection con = DriverManager.getConnection(dbAddress, dbUser, dbUserPassword);
           java.sql.Statement stmt = con.createStatement();
           ResultSet rs = stmt.executeQuery(query);
        
           while (rs.next()){
               BusinessCase newCase = new BusinessCase();
               
               newCase.setCaseID(rs.getInt("BusinessCaseID"));
               newCase.setBuyerID(rs.getInt("ID_User"));
               newCase.setImmoID(rs.getInt("ID_IMMO"));
               newCase.setTimestamp(rs.getString("BusinessCaseDate"));


               newResultVector.add(newCase);
           }
        
        } catch (SQLException ex) {
            Logger.getLogger(ImmoDAO.class.getName()).log(Level.SEVERE, null, ex);
        }    

        
        
        
        
        
        
        return allBusinessCases;
    }
    
}
